package com.sms.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sms.graduate.dao.SmsWorkDao;
import com.sms.graduate.entity.SmsWork;
import com.sms.graduate.entity.dto.WorkDTO;
import com.sms.graduate.service.SmsWorkService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @author jc
 */
@Service
public class SmsWorkServiceImpl extends ServiceImpl<SmsWorkDao, SmsWork> implements SmsWorkService {

    @Override
    public Boolean addWork(WorkDTO workDTO, String usercode) {
        SmsWork smsWork = new SmsWork();
        smsWork.setEndTime(workDTO.getEndTime());
        smsWork.setStartTime(workDTO.getStartTime());
        smsWork.setTeacherCode(usercode);
        smsWork.setWorkName(workDTO.getWorkName());
        return this.save(smsWork);
    }

    @Override
    public SmsWork selectWorkByName(String teachercode,String name) {
        QueryWrapper<SmsWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsWork.COL_WORKNAME,name)
                .eq(SmsWork.COL_TEACHERCODE,teachercode);
        SmsWork one = this.getOne(queryWrapper);
        return one;
    }

    @Override
    public Boolean selectWorkByTime(String workName){
        QueryWrapper<SmsWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsWork.COL_ID,workName);
        SmsWork one = this.getOne(queryWrapper);
        long time = System.currentTimeMillis();
        long time1 = one.getEndTime().getTime();
        long time2 = one.getStartTime().getTime();
        if (time>=time1&&time1<=time2){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteWorkById(Integer id) {
        return  this.removeById(id);
    }

    @Override
    public SmsWork selectWorkById(Integer id) {
        SmsWork byId = this.getById(id);
        return byId;
    }

    @Override
    public List<SmsWork> workListByTeacherCode(String teacherCode) {
        QueryWrapper<SmsWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsWork.COL_TEACHERCODE,teacherCode);
        return this.list(queryWrapper);
    }

    @Override
    public void addSubmitNum(String workName) {
        UpdateWrapper<SmsWork> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("submit_num=submit_num+1")
                .eq(SmsWork.COL_WORKNAME,workName);
        this.update(updateWrapper);
    }

    @Override
    public Boolean updateWork(SmsWork smsWork) {

        return null;
    }

    @Override
    public List<SmsWork> workList(String teacherCode) {
        QueryWrapper<SmsWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsWork.COL_TEACHERCODE,teacherCode)
                .orderByDesc(SmsWork.COL_ENDTIME);
        return this.list(queryWrapper);
    }

    @Override
    public List<SmsWork> studentWorkList(List<String> list) {
        QueryWrapper<SmsWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(SmsWork.COL_TEACHERCODE,list)
                .orderByDesc(SmsWork.COL_ENDTIME);
        List<SmsWork> list1 = this.list(queryWrapper);
        return list1;
    }

}

